package net.chathu.airgw.packets.gw.clientpackets 
{
	import net.chathu.airgw.Debug;
	import net.chathu.airgw.model.JoinedRoomInfo;
	import net.chathu.airgw.model.PrivateChatInfo;
	import net.chathu.airgw.model.Room;
	import net.chathu.airgw.model.User;
	import net.chathu.airgw.packets.gw.serverpackets.EchoPacket;
	import net.chathu.airgw.packets.gw.serverpackets.ErrorNoSuchChannelPacket;
	import net.chathu.airgw.packets.gw.serverpackets.ErrorNoSuchNickOrChannelPacket;
	import net.chathu.airgw.util.ByteArrayUtil;
	import net.chathu.airgw.util.StringUtil;
	/**
	 * ...
	 * @author ze
	 */
	public class CMDPrivMsg extends IRCReceivablePacket 
	{
		
		public function CMDPrivMsg() 
		{
			
		}
		
		override public function execute():void {
			if (data.length > 2)
			{
				var target:String = data[1];
				var line:String = data.join(" ");
				
				//az 2. szokoz utan kezodik az uzenet
				var firstSpacePos:int = line.indexOf(" ");
				var secondSpacePos:int = line.indexOf(" ",firstSpacePos+1);
				var message:String = line.substr(secondSpacePos + 1);
				//xchat csak a : karakterrel kezdodo szoveg ele rak kettospontot,
				//mig mirc mindenhova
				if (message.indexOf(":") == 0) {
					message = message.substr(1);
				}

				
				if (target.indexOf("#") == 0)
				{
					//TODO: konferenciaszoba kezeles!
					//egy szobaba irnank uzenetet
					var room:Room = sessionData.chatData.rooms.getRoomByName(target);
					if (room)
					{
						//ellenorizzuk, hogy az adott szobaban vagy-e
						if (sessionData.chatData.isInRoom(room))
						{
							/*
							 * /me parancsok kezelese:
							 */
							
							// 
							//15 vagy 1 charcode jon? 
							if (message.indexOf("ACTION") == 1 && (message.charCodeAt(0) == 15 || message.charCodeAt(0) == 1))
							{
								message = "ACTION" + message.substring(7);
							}
							
							
							//kicsereljuk a felkover, alahuzott, szinezett karakter kodjat
							
							message = StringUtil.replaceIRCStringFormatToSpecialChars(message);
							
							//Ez java kodbol lett atemelve, ellenorizni kell, mire is jo <-> jo-e vmire!
							if (message.indexOf("ACTION") == 0 && String(data[2]).indexOf(":ACTION") != 0)
							{
								sendToChat(ByteArrayUtil.convertParams(["m","rooms/" + room.id + "/chat","/me " + message.substring(7)]));
							} else {
								sendToChat(ByteArrayUtil.convertParams(["m","rooms/" + room.id + "/chat", message]));
							}
						} else {
							sendToClient(new EchoPacket(sessionData.nick, "Nem léptél be a(z) " + room.name + " szobába!"));
						}
					} else {
						sendToClient(new ErrorNoSuchNickOrChannelPacket(sessionData.nick, target));
					}
				} else if (target.indexOf("&") == 0) {
					if (sessionData.isInGroup(target))
					{
						
						sessionData.soul.p2pChatConnection.sendMessage(target, message);
					}else {
						sendToClient(new EchoPacket(sessionData.nick, "Nem léptél be a(z) " + room.name + " szobába!"));
					}
				} else {
					//message = "ACTION test"
					// charCode: 1 (nem feltetlenul 15?)
					if (message.indexOf("ACTION") == 1 && (message.charCodeAt(0) == 15 || message.indexOf("")==0))
					{
						message = "/me " + message.substring(7);
					}
					
					var partner:User = User(sessionData.chatData.users.getUserByNameIgnoreCase(target));
					
					if (partner)
					{
						var privInfo:PrivateChatInfo = sessionData.chatData.getPrivateChatInfoByPartnerId(partner.id);
						
						if (privInfo)
						{
							//ha mar letezik a privat beszelgetes
							sendToChat(ByteArrayUtil.convertParams(["m","private/" + privInfo.privateId,message]));
						} else {
							//queue-ba tesszuk az uzenetet, letrehozzuk a privat beszelgetest
							sessionData.chatData.privateMessageQueue.addMessageToQueue(partner.id, message);
							sendToChat(ByteArrayUtil.convertParams(["m","private","-1",partner.id]));
						}
					} else {
						sendToClient(new ErrorNoSuchNickOrChannelPacket(sessionData.nick, target));
					}
					
				}
			}
			
			
		}
		public static function getShortUsageDescription():String {
			return "Üzenetküldés";
		}
		
		public static function getUsageDescription():String {
			return "Példa:\n" +
				"/privmsg fül_elek hello\n\n" +
				"Eredmény:\n" + 
				"[gipsz_jakab] hello\n\n" +
				"Sikeresen üzenetet írtál fül_elek felhasználónak.";
		}
	}
}